<?php


namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use think\Db;

/**
 * 新闻公告模块
 */
class News extends XilujobApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * @ApiTitle    (公告)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xilujob.news/announcement)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','title':'string','thumb':'string'}", description="返回数据")
     */
    public function announcement(){
        $pagesize = $this->request->param('pagesize');
        $pagesize = $pagesize ?? 10;
        $list = \app\common\model\xilujob\News::field(['id','title','thumb'])->where('is_recommend',1)->where('status',1)->order('top','desc')->paginate($pagesize);
        $this->success('查询成功', $list);
    }

    public function lists(){
        $category_id = $this->request->param('category_id');
        $lists = \app\common\model\xilujob\News::field(['id','title','thumb','createtime'])->where('status',1)->where('news_category_id',$category_id)->order('top','desc')->paginate();
        $this->success('查询成功', $lists);
    }

    public function detail() {
        $id = $this->request->param('id');
        $news = \app\common\model\xilujob\News::get($id);
        if(!$news || $news->status!=1) {
            $this->error(__('no record'));
        }
        \app\common\model\xilujob\News::update(['view_count'=>Db::raw('view_count+1')],['id'=>$news->id]);
        $this->success('', ['news'=>$news]);
    }

}